import type { AccountStandardsType } from '@/apis/eci/emissionAccounting/model/inventoryTaskModel';
import { CarbonInventoryStandardsMap } from '@/constants/enum';
import { isJSON } from '@/utils';
import { isArray } from './is';

/**
 * 处理标准格式
 * @param accountStandards 接口返回的json字符串
 * @returns
 */
export function handleStandardsFormat(accountStandards: string | any[]) {
  if (!accountStandards || accountStandards.length == 0) return '-';
  let str = '';
  if (isArray(accountStandards)) {
    for (let i = 0; i < accountStandards.length; i++) {
      const item = accountStandards[i];
      str += `${i > 0 ? '，' : ''}${
        CarbonInventoryStandardsMap[item.standard as keyof typeof CarbonInventoryStandardsMap]
      }${item.gwpVersion ? `(${item.gwpVersion})` : ''}`;
    }
    return str;
  }
  if (isJSON(accountStandards as string)) {
    const accountStandardsArr: AccountStandardsType[] = JSON.parse(accountStandards as string);
    for (let i = 0; i < accountStandardsArr.length; i++) {
      const item = accountStandardsArr[i];
      str += `${i > 0 ? '，' : ''}${CarbonInventoryStandardsMap[item.standard]}${
        item.gwpVersion ? `(${item.gwpVersion})` : ''
      }`;
    }
    return str;
  }

  return accountStandards;
}
